package uutalk.netcloud.talk.app.ui.Activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.makeramen.roundedimageview.RoundedImageView;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.io.ByteArrayOutputStream;

import butterknife.BindView;
import butterknife.OnCheckedChanged;
import butterknife.OnClick;
import uutalk.netcloud.talk.R;
import uutalk.netcloud.talk.base.android.BaseActivity;
import uutalk.netcloud.talk.base.mqtt.MQTTManager;
import uutalk.netcloud.talk.base.mqtt.PublishFactory;
import uutalk.netcloud.talk.base.mqtt.event.MqttMessageEvent;
import uutalk.netcloud.talk.base.mqtt.model.Publication;
import uutalk.netcloud.talk.base.util.ZxingUtils;
import uutalk.netcloud.talk.base.util.thirdpart.EventMamager;
import uutalk.netcloud.talk.base.util.thirdpart.ImageLoader;
import uutalk.netcloud.talk.base.util.thirdpart.QiNiuManager;
import uutalk.netcloud.talk.base.util.thirdpart.UserManager;
import uutalk.netcloud.talk.base.util.view.ToastUtil;
import uutalk.netcloud.talk.model.BussinessBeanFactory;
import uutalk.netcloud.talk.model.bean.CMDConstant;
import uutalk.netcloud.talk.model.bean.CreatActBean;
import uutalk.netcloud.talk.model.bean.CreatActRspBean;
import uutalk.netcloud.talk.model.bean.LoginInfo;
import uutalk.netcloud.talk.model.bean.SimpleEventType;
import uutalk.netcloud.talk.model.bean.UserInfo;

import static uutalk.netcloud.talk.R.id.act_content;
import static uutalk.netcloud.talk.R.id.password_layout;
import static uutalk.netcloud.talk.R.id.st_encode;

/**
 * Created by pmt
 */

public class CreateActActivity extends BaseActivity {

    @BindView(R.id.act_name)
    EditText mActName;
    @BindView(st_encode)
    Switch mStEncode;

    @BindView(password_layout)
    LinearLayout mPasswordLayout;

    @BindView(R.id.act_password)
    EditText mActPassword;
    @BindView(R.id.st_sure)
    Switch mStSure;
    @BindView(act_content)
    EditText mActContent;

    @BindView(R.id.headIV)
    RoundedImageView headIV;

    @BindView(R.id.nameTV)
    TextView nameTV;

    UserInfo userInfo = null;

    @Override
    public int layoutId() {
        return R.layout.activity_create_act;
    }

    @Override
    public void init() {
        userInfo = UserManager.getInstance().getUserInfo();
        ImageLoader.loadImage(mActivity, QiNiuManager.QINIU_HOST + userInfo.getPicture(), headIV);
        nameTV.setText(userInfo.getName());
//        mPasswordLayout.setVisibility(View.);
    }

    @Override
    public boolean autoBindEvent() {
        return true;
    }

    @OnCheckedChanged(st_encode)
    public void checkEncode()
    {
        if(mStEncode.isChecked())
        {
            mPasswordLayout.setVisibility(View.VISIBLE);
        }
        else
        {
            mPasswordLayout.setVisibility(View.GONE);
        }
    }


    @OnClick(R.id.act_create)
    public void onClickCreateAct()
    {
        if(mActName.getText().toString().trim().equals("")){
            Toast.makeText(mActivity, "请输入群组名称", Toast.LENGTH_SHORT).show();
            return;
        }
        if(mStEncode.isChecked()&&mActPassword.getText().toString().trim().equals("")){
            Toast.makeText(mActivity, "请输入群组密码", Toast.LENGTH_SHORT).show();
            return;
        }
        //点击创建活动
        String actName = mActName.getText().toString();
        String actPassword = mActPassword.getText().toString();
        String actContent = mActContent.getText().toString();
        boolean isEncode = mStEncode.isChecked();
        boolean isSure = mStSure.isChecked();

        CreatActBean creatActBean = new CreatActBean();

        creatActBean.setGroupId("");
        creatActBean.setGroupName(actName);
        creatActBean.setGroupInfo(actContent);
        creatActBean.setGroupPassword(actPassword);
        creatActBean.setPwdAuth(isEncode?"1":"0");
        creatActBean.setOwnerVerify(isSure?"1":"0");

        if (MQTTManager.getInstance().isConnected()) {
            progressHudUtil.showProgressHud();
            LoginInfo loginInfo = UserManager.getInstance().getLoginInfo();
            creatActBean.setUserId(loginInfo.getDeviceId());
            Publication publication = PublishFactory.createPublication(BussinessBeanFactory.createActivityBean(creatActBean));
            MQTTManager.getInstance().publish(publication);
        }
        else
        {
            ToastUtil.makeToast(R.string.request_error);
        }

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMqttMessageEvent(MqttMessageEvent event) {
        if (CMDConstant.CREATE_ACTIVE_GROUP_RESP_CMD.equals(event.getMessage().getCmd())) {
            progressHudUtil.dismissProgressHud();
            CreatActRspBean creatActRspBean = JSON.parseObject(event.getMessage().toString(), CreatActRspBean.class);
            if (creatActRspBean.isOk()) {
                EventMamager.getInstance().postEvent(SimpleEventType.CREATE_ACTIVE_GROUP_RESP);
//                Intent intent = new Intent(mActivity, BlueToothActivity.class);
//                startActivity(intent);
//               finish();
                //放在handle
                String createGroupResStr = "HD"+creatActRspBean.getGroupId();
                Bitmap bit = ZxingUtils.encodeAsBitmap(createGroupResStr);
                Intent intentShare=new Intent(CreateActActivity.this, ActShareActivity.class);
                ByteArrayOutputStream baos=new ByteArrayOutputStream();
                bit.compress(Bitmap.CompressFormat.PNG, 100, baos);
                byte [] bitmapByte =baos.toByteArray();
                intentShare.putExtra("bitmap", bitmapByte);
                intentShare.putExtra("group_id", creatActRspBean.getGroupId());
                intentShare.putExtra("group_name", mActName.getText().toString());
                startActivity(intentShare);
                finish();
            } else {
                ToastUtil.makeToast(creatActRspBean.getError_msg());
            }
        }
    }



}
